import { Application } from "egg";
import * as AutoIncrementFactory from "mongoose-sequence";
import { ObjectId } from "mongoose";
export interface CompanyProps {
  _id: ObjectId;
  id?: number;
  company_name: string; // 公司名称
  is_vip: boolean; // 是否是VIP
  vip_expired: number; // VIP过期时间
  children: Array<string>; // 子公司
  is_delete: boolean; // 是否已删除
}
function initCompanyModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const CompanyScheme = new Schema<CompanyProps>(
    {
      company_name: { type: String, unique: true, required: true },
      is_vip: { type: Boolean, default: false },
      vip_expired: { type: Number, default: -1 },
      children: { type: Array },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  CompanyScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Company_counter",
  });
  return app.mongoose.model<CompanyProps>("Company", CompanyScheme);
}

export default initCompanyModel;
